package cczxsong;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class PercolationStats {

    public double[] array;
    public int N,T;
    public double sum=0,avg=0,var=0;

    public void PercolationStats_x(int N,int T) {
        this.T = T;
        this.N = N;

        array = new double[T];
        for (int i = 0 ; i < T ; i ++){
            OneExp ww = new OneExp();
            array[i] = ww.OneExperiment(N);
        }
    }   // perform T independent computational experiments on an N-by-N grid
    public double mean(){
        StdOut.println("   ");
        for (int i = 0 ; i < T ; i++){
            sum += array[i];
        }
        avg = sum/T;
        StdOut.println("均值为:");
        StdOut.println(avg);
        return avg;
    }         // sample mean of percolation threshold

    public double stddev(){
        for ( int i = 0 ; i < T ; i ++){
            var += (array[i]-avg)*(array[i]-avg);
        }
        var /= T;
        var =Math.sqrt(var);
        StdOut.println("标准差为:");
        StdOut.println(var);
        return var;
    }          // sample standard deviation of percolation threshold
    public double confidenceLo()  {
        StdOut.println("置信区间下限：");
        return avg-1.96*var;
    }       // returns lower bound of the 95% confidence interval
    public double confidenceHi()   {
        StdOut.println("置信区间上限：");
        return avg+1.96*var;
    }      // returns upper bound of the 95% confidence interval
    public static void main(String[] args)   {
        StdOut.println("请输入方格的规格：");
        int N = StdIn.readInt();
        StdOut.println("请输入实验的次数：");
        int T = StdIn.readInt();

        long starttime = System.currentTimeMillis();
        PercolationStats q = new PercolationStats();
        q.PercolationStats_x(N,T);
        long endtime = System.currentTimeMillis();
        StdOut.println("运行时间为:"+(endtime-starttime)+"ms");

        q.mean();
        q.stddev();
        StdOut.println(q.confidenceLo());
        StdOut.println(q.confidenceHi());
    } // test client, described below
}

